package com.project.erp.dao.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.DocNumber;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：系统-系统编号生成

 *  功能描述：系统-系统编号Dao类

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-12-11 下午10:38:52
 */
@Repository
public class DocNumberDao extends HibernateDao<DocNumber, Long> {
	
	/**
	 * 根据条件取得编号最大值
	 * @param docNumber
	 * @return
	 */
	public DocNumber getDocNumber(DocNumber docNumber){
		Page<DocNumber> page = new Page<DocNumber>();
		page.setPageNo(1);
		page.setPageSize(1);
		page.setOrderBy("numCount");
		page.setOrder(Page.DESC);
		
		List<Criterion> list = new ArrayList<Criterion>();
		if(StringUtils.isNotEmpty(docNumber.getDocName())){
			Criterion docName = Restrictions.eq("docName",docNumber.getDocName());
			list.add(docName);
		}
		if(StringUtils.isNotEmpty(docNumber.getGeneYear())){
			Criterion geneYear = Restrictions.eq("geneYear",docNumber.getGeneYear());
			list.add(geneYear);
		}
		if(StringUtils.isNotEmpty(docNumber.getGeneMonth())){
			Criterion geneMonth = Restrictions.eq("geneMonth",docNumber.getGeneMonth());
			list.add(geneMonth);
		}
		if(StringUtils.isNotEmpty(docNumber.getGeneDay())){
			Criterion geneDay = Restrictions.eq("geneDay",docNumber.getGeneDay());
			list.add(geneDay);
		}

		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		
		List<DocNumber> docNumberList = page.getResult();
		if((docNumberList != null) && (docNumberList.size() > 0)){
			return page.getResult().get(0);
		}else{
			return null;
		}
	}
}
